/**
 * @author Justin Akin
 * @version Action Pals Core Library 0.1.0
 */
package com.actionpals.broadcasters
{	
	import flash.display.InteractiveObject;
	import flash.ui.Keyboard;
	import flash.events.KeyboardEvent;
	import com.actionpals.events.KeyScrollEvent;
	import com.actionpals.base.BaseEventDispatcher;
	
	/**
	 * 
	 */
	public class KeyScrollCast extends AbstractScrollCast
	{
		
	//--------------------------------------------------------------------------------
	//
	//	Contstructor
	//
	//--------------------------------------------------------------------------------
	
		/**
		 * Constructor.
		 */
        public function KeyScrollCast( eventTarget:InteractiveObject )
        {	
        	super( eventTarget );        	
        	init();
        }
		
	//--------------------------------------------------------------------------------
	//
	//	Protected Functions
	//
	//--------------------------------------------------------------------------------
	
		/**
		 * 
		 */
		protected function init():void
		{	
			_eventTarget.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
			_eventTarget.addEventListener( KeyboardEvent.KEY_UP, onKeyUp );	
		}
		
		/**
		 * 
		 */
		protected function getEventType( event:KeyboardEvent ):String
		{	
			var type:String = "";
			var keyCode:uint = event.keyCode;
			var shiftKey:Boolean = event.shiftKey;
			
			switch( keyCode )
			{
				case Keyboard.UP :
				
					type = ( shiftKey ) ? KeyScrollEvent.PAGE_UP : KeyScrollEvent.ARROW_UP;
					break;
					
				case Keyboard.DOWN :
					
					type = ( shiftKey ) ? KeyScrollEvent.PAGE_DOWN : KeyScrollEvent.ARROW_DOWN;
					break;
					
				case Keyboard.LEFT :
					
					type = ( shiftKey ) ? KeyScrollEvent.HOME : KeyScrollEvent.ARROW_LEFT;
					break;
					
				case Keyboard.RIGHT :
				
					type = ( shiftKey ) ? KeyScrollEvent.END : KeyScrollEvent.ARROW_RIGHT;
					break;
			}
			
			return type;
		}
		
	//--------------------------------------------------------------------------------
	//
	//	Events Handlers
	//
	//--------------------------------------------------------------------------------
		
		/**
		 * 
		 */
		protected function onKeyUp( event:KeyboardEvent ):void
		{	
			var ksType:String = getEventType( event );
			var ksEvent:KeyScrollEvent = new KeyScrollEvent( ksType, _eventsBubble, _eventsCancelable, false );
			
			if( ksType != "" ) dispatchEvent( ksEvent );
		}
		
		/**
		 * 
		 */
		protected function onKeyDown( event:KeyboardEvent ):void
		{	
			var ksType:String = getEventType( event );
			var ksEvent:KeyScrollEvent = new KeyScrollEvent( ksType, _eventsBubble, _eventsCancelable, false );
			
			if( ksType != "" ) dispatchEvent( ksEvent );	
		}		
	}	
}